package com.chen.blue.controller.admin.system;


import com.chen.blue.annotation.permission.DataPermission;
import com.chen.blue.domain.delete.system.UserDelete;
import com.chen.blue.domain.dto.system.UserDto;
import com.chen.blue.domain.dto.system.update.UserDtoEdit;
import com.chen.blue.domain.query.system.UserQuery;
import com.chen.blue.domain.vo.system.UserVo;
import com.chen.blue.exception.e.DtoNullException;
import com.chen.blue.result.DataResult;
import com.chen.blue.result.R;
import com.chen.blue.service.system.IUsersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author CLT
 * @since 2024-04-27
 */
@Api(tags = "用户模块")
@Validated
@RestController
@RequestMapping("/user")
public class UsersController {

    @Resource
    private IUsersService userService;

    @ApiOperation(value = "用户添加", notes = "用户添加")
    @PostMapping("/add")
    public R add(@RequestBody @Valid UserDto userDto) throws DtoNullException {

        return userService.addUser(userDto) ? R.success() : R.faild();

    }

    @ApiOperation(value = "用户删除", notes = "用户删除")
    @DeleteMapping("/delete")
    public R delete(@RequestBody UserDelete userDelete) throws DtoNullException {

        return userService.deleteUser(userDelete) ? R.success() : R.faild();

    }

    @ApiOperation(value = "用户修改", notes = "用户修改")
    @PutMapping("/edit")
    public R updateById(@RequestBody @Valid UserDtoEdit userDtoEdit) throws DtoNullException {

        return userService.updateUserById(userDtoEdit) ? R.success() : R.faild();

    }

    @DataPermission
    @ApiOperation(value = "多条件查询用户(已配数据权限)", notes = "多条件查询用户(已配数据权限)")
    @PostMapping("/query")
    public R<DataResult<List<UserVo>>> query(@RequestBody UserQuery userQuery) throws DtoNullException {

        return R.success(userService.queryUser(userQuery));

    }

    @ApiOperation(value = "ID查询用户", notes = "ID查询用户")
    @GetMapping("/queryById/{id}")
    public R<DataResult<UserVo>> queryById(@PathVariable("id") Long id) throws DtoNullException {

        return R.success(userService.queryUserById(id));

    }

    @ApiOperation(value = "当前用户信息", notes = "当前用户信息")
    @GetMapping("/query/userInfo")
    public R<DataResult<Map<String, Object>>> queryCurrentUserInfo() throws DtoNullException {

        return R.success(userService.getCurrentUserInfo());

    }

}

